package com.hj.saapp.control.fragment.base;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;


import com.hj.saapp.R;
import com.hj.saapp.common.event.EventExit;
import com.hj.saapp.common.manager.NetworkManager;
import com.hj.saapp.control.activity.base.BaseActivity;
import com.hj.saapp.control.adapter.CommonAdapter;
import com.hj.saapp.model.library.PullToRefreshBase;
import com.hj.saapp.model.library.PullToRefreshListView;
import com.hj.saapp.view.BaseFragmentView;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import lib.hj.com.commonlib.bean.BaseBean;
import lib.hj.com.commonlib.utils.OnAsyncHttpResponse;
import lib.hj.com.commonlib.utils.Req;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseFragment extends Fragment implements OnAsyncHttpResponse {

    public BaseFragmentView mRootView;
    public ArrayList mData;
    public PullToRefreshListView queryLv;
    public CommonAdapter adapter;
    public BaseActivity activity;
    public int pageSize = 10;
    public int pageTotal = 2;
    public boolean isLoad = true;
    public boolean isLoading = false;
    public boolean isPulltoLoad = false;
    public JSONObject json;
    public  int opration = -1;


    public BaseFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        EventBus.getDefault().register(this);

        if (mRootView == null){
            mRootView = new BaseFragmentView(this);

        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null){
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (activity == null) {
            activity = (BaseActivity) this.getActivity();
            json = new JSONObject();
            queryLv = (PullToRefreshListView) mRootView.findViewById(R.id.queryLv);
            if (queryLv != null) {
                mData = new ArrayList();
                adapter = getAdapter();
                queryLv.setAdapter(adapter);
                queryLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                    @Override
                    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                        isPulltoLoad = true;
                        opration =1;
                        prepareLoadListData();
                    }

                    @Override
                    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                        isPulltoLoad = false;
                        opration =1;
                        prepareLoadListData();
                    }
                });
            }
            initView();
            initListener();
            initData();
        }


    }

    public void initView()
    {

    }
    public void initData()
    {

    }
    public void initListener()
    {

    }

    public void  prepareLoadData() {

        try {

        } catch (Exception e) {

        }

        if (NetworkManager.getNetworkManager().isNetworkConnected()) {

            if (NetworkManager.getNetworkManager().isGetHost())
            {
                if (mRootView !=null)
                {
                    mRootView.showLoading();
                }

                try {
                    getHttpData();

                } catch (Exception e) {

                }
            }
            else
            {
                listRefreshComplete();
            }
        }else
        {
            listRefreshComplete();
            if (queryLv != null && mData.size() > 0) {
                mRootView.showContent();
            } else {

                if (queryLv != null) {
                    mRootView.showEmpty();
                }
                

            }
            activity.showMessageNoNetWork();
        }


    }

    public void  prepareLoadListData() {


        try {

        } catch (Exception e) {

        }

        if (NetworkManager.getNetworkManager().isNetworkConnected()) {

            if (NetworkManager.getNetworkManager().isGetHost())
            {
                if (mData.size() == 0)
                {
                    mRootView.showLoading();
                }

                try {
                    getListData();
                } catch (Exception e) {

                }
            }
            else
            {
                listRefreshComplete();
            }

        }
        else
        {
            listRefreshComplete();
            if (queryLv != null && mData.size() > 0) {
                mRootView.showContent();
            } else {
                mRootView.showEmpty();
            }
            activity.showMessageNoNetWork();

        }


    }





    @Override
    public void onSuccess(BaseBean paramObject) {
        listRefreshComplete();
        mRootView.showContent();
    }

    @Override
    public void onFailure(int paramInt, String paramString) {

        if (queryLv != null && mData.size() > 0) {
            listRefreshComplete();
            mRootView.showContent();
        } else {
            mRootView.showEmpty();
        }

        if (activity.checkSign(paramInt)) {

            if (paramInt > 1000) {

                activity.showPopDialog(paramString);
            }

            return;
        }
        activity.showPopDialog(paramString);

    }

    @Override
    public void offLine(Req req) {

    }

    public CommonAdapter getAdapter()
    {
        return  null;
    }
    public int getTitleById()
    {
        return 0;
    }
    public int getLayoutByid()
    {
        return 0;
    }
    public void getHttpData() throws Exception{

    }

    public void getListData() {

        try {
            if (isPulltoLoad) {
                json.put("page", 1);
            } else {
                json.put("page", (mData.size() / pageSize + 1));
            }

            json.put("pageSize", pageSize);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (isPulltoLoad) {
            try {
                isLoading = true;
                getHttpData();
            } catch (Exception e) {

            }

            return;
        }

        if (isLoad && pageTotal != 1) {

            if ((mData.size() / pageSize + 1) == pageTotal) {
                isLoad = false;
            }

            try {
                isLoading = true;
                getHttpData();
            } catch (Exception e) {

            }


        } else {

            listRefreshComplete();
        }
    }

    public void  listRefreshComplete()
    {
        activity.baseHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                if (queryLv != null) {

                    queryLv.onRefreshComplete();
                }

            }
        }, 200);
    }

    public String getMethod()
    {
        return "";
    }

    public void onPositiveClickListener(String type, String text)
    {

    }
    public void onNegativeClickListener(String type, String text)
    {


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 退出
     *
     * @param event
     */
    public void onEventMainThread(EventExit event) {



    }
}
